package com.app.controller;

import com.alibaba.excel.EasyExcel;
import com.app.domain.dto.ExcelMapHandleReqDto;
import com.app.service.EasyExcelTestService;
import com.app.utils.BaseDataExcelImportListener;
import com.app.utils.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * EasyExcel test
 *
 * @author ch
 * @date 2023/1/12 14:24
 */
@RestController
@RequestMapping("/easyexcel")
@AllArgsConstructor
public class EasyExcelTestController {

    private final EasyExcelTestService easyExcelTestService;

    /**
     * 上传文件
     */
    @PostMapping("upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return R.error("文件为空");
        }
        BaseDataExcelImportListener.CACHE.clear();
        EasyExcel.read(file.getInputStream(), new BaseDataExcelImportListener()).sheet().doRead();
        return R.ok();
    }

/*
-- apifox参数格式：
tableName:
rules[0].columnName:
rules[0].columnType:
rules[0].excelHeadName:
rules[1].columnName:
rules[1].columnType:
rules[1].excelHeadName:
rules[2].columnName:
rules[2].columnType:
rules[2].excelHeadName:
rules[3].columnName:
rules[3].columnType:
rules[3].excelHeadName:
*/
    /**
     * 导入执行处理
     */
    @PostMapping("uploadHandle")
    public R uploadHandle(ExcelMapHandleReqDto param) {
        easyExcelTestService.uploadHandle(param);
        return R.ok();
    }

}
